package com.szewec.ps.util.map;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;

import com.szewec.ps.base.BaseApplication;
import com.szewec.ps.util.LogUtil;

/**
 * 地图相关工具类
 */
public class MapUtils {
    private static final String TAG = "MapUtils";
    private static final double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    public static double pi = 3.1415926535897932384626;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    /**
     * 设置坐标
     */
    public static Double[] getCoordinate(){
        try {
            LocationManager locationManager = (LocationManager) BaseApplication.getContext().getSystemService(Context.LOCATION_SERVICE);
            if (locationManager == null){
                return null;
            }
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);//低精度，如果设置为高精度，依然获取不了location。
            criteria.setAltitudeRequired(false);//不要求海拔
            criteria.setBearingRequired(false);//不要求方位
            criteria.setCostAllowed(true);//允许有花费
            criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗

            //从可用的位置提供器中，匹配以上标准的最佳提供器
            String locationProvider = locationManager.getBestProvider(criteria, true);
            if (locationProvider == null){
                return null;
            }
            Location location = locationManager.getLastKnownLocation(locationProvider);
            if (location != null) {
                return gps84ToBD09(location.getLongitude(),location.getLatitude());
            }else {
                LogUtil.e(TAG,"获取位置信息失败");
                return null;
            }
        }catch (SecurityException e){
            LogUtil.e(TAG,e);
        }catch (Exception e){
            LogUtil.e(TAG,e);
        }
        return null;
    }

    /**
     * 将定位坐标转换为百度坐标
     */
    public static Double[] gps84ToBD09(Double xcoordinate,Double ycoordinate){
        if (xcoordinate == null || ycoordinate == null){
            LogUtil.e(TAG,"坐标参数异常");
            return null;
        }
        //84坐标转gcj02
        double lon = xcoordinate;
        double lat = ycoordinate;
        double dLat = transformLat( lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        //gcj02转bd09
        double x = mgLon, y = mgLat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta) + 0.0065;
        double tempLat = z * Math.sin(theta) + 0.006;
        return  new Double[]{tempLon,tempLat};
    }

    public static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    public static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return ret;
    }
}
